/*
 * Copyright (c) 2022  by Junzhao
 * THIS FILE IS PART OF JAVASE Song Huongkang PROJECT
 * All Rights Reserved.
 */

package 练习.练习二;
/**
 * @description 存在线程安全问题 解决 ：通过同步机制
 *          一同步代码块 synchronized(同步监视器){要同步的的代码(即需要被同步的代码)}
 *                      共享数据：多个线程共同操作的数据
 *                      同步监视器：（锁）任何一个类的对象都可以充当锁
 *                          要求多个线程共用同一把
 *          二同步方法

 * @author 俊昭
 * @date 2022/4/12
*/

public class WindowsTestUpdate {
    public static void main(String[] args) {
        WindowsUpdate wu = new WindowsUpdate();
        Thread t1 = new Thread(wu,"窗口1");
        Thread t2 = new Thread(wu,"窗口2");
        Thread t3 = new Thread(wu,"窗口3");

        t1.start();
        t2.start();
        t3.start();
    }
}
class WindowsUpdate implements Runnable{
    //    Object obj = new Object();
    private int ticket = 1000;

    @Override
    public void run() {
        while(true){
            synchronized (this){
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (ticket>0){
                    System.out.println(Thread.currentThread().getName()+"售票 x1 号码为："+ticket);
                    ticket--;
                }else{
                    break;
                }
            }
        }
    }
}